package net.northcoding.fresh.util;
// @Author Diamond - Administrator
// @Date 2023/12/8 12:02:40

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Logger {
    public static final int DEBUG = 1;
    public static final int INFO = 2;
    public static final int WARN = 3;
    public static final int ERROR = 4;
    public static final int FAIL = 5;
    private String name;
    private int level = 2;
    private String LogPath = "D:\\code\\Java\\others\\Fresh\\code\\output.log";
    public Logger(String name) {
        this.name = name;
    }
    public Logger(String name, String logPath) {
        this.name = name;
        this.LogPath = logPath;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public void FileOutput(String context) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(LogPath);
        byte[] bytes = fileInputStream.readAllBytes();
        fileInputStream.close();
        FileOutputStream fileOutputStream = new FileOutputStream(LogPath);
        fileOutputStream.write(bytes);
        fileOutputStream.write("\n".getBytes());
        fileOutputStream.write(context.getBytes());
        fileOutputStream.close();
    }

    //    private boolean useFile = false;
    public void log(int level, String content) throws IOException {
        if (this.level > level) {
            return;
        }
        switch (level) {
            case 1:
                debug(content);
                break;
            case 2:
                info(content);
                break;
            case 3:
                warn(content);
                break;
            case 4:
                error(content);
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + level);
        }
    }

    public void debug(String content) throws IOException {// -[YYYY/MM/dd HH/MM/ss] D xxx
        if (level <= 1)
            System.out.printf(" -%s %s %s %s\n",
                    ConsoleUtil.getColoredString(ConsoleUtil.color.GREEN, false, ConsoleUtil.font.LESS, ConsoleUtil.getTime()),
                    ConsoleUtil.getColoredString(ConsoleUtil.color.CYAN, false, ConsoleUtil.font.LESS, name),
                    ConsoleUtil.getColoredString(ConsoleUtil.color.GRAY, true, ConsoleUtil.font.BOLD, "D"),
                    content
            );
        if (LogPath != null) {
//            FileOutput("-"+ConsoleUtil.getTime()+" "+ name +" [D] "+content);
        }
    }

    public void info(String content) throws IOException {// -[YYYY/MM/dd HH/MM/ss] I xxx
        if (level <= 2)
            System.out.printf(" -%s %s %s %s\n",
                    ConsoleUtil.getColoredString(ConsoleUtil.color.GREEN, false, ConsoleUtil.font.LESS, ConsoleUtil.getTime()),
                    ConsoleUtil.getColoredString(ConsoleUtil.color.CYAN, false, ConsoleUtil.font.LESS, name),
                    ConsoleUtil.getColoredString(ConsoleUtil.color.BLUE, true, ConsoleUtil.font.BOLD, "I"),
                    content
            );
        if (LogPath != null) {
//            FileOutput("-"+ConsoleUtil.getTime()+" "+name+" [I] "+content);
        }
    }

    public void warn(String content) throws IOException {// -[YYYY/MM/dd HH/MM/ss] I xxx
        if (level <= 3)
            System.out.printf(" -%s %s %s %s\n",
                    ConsoleUtil.getColoredString(ConsoleUtil.color.GREEN, false, ConsoleUtil.font.LESS, ConsoleUtil.getTime()),
                    ConsoleUtil.getColoredString(ConsoleUtil.color.CYAN, false, ConsoleUtil.font.LESS, name),
                    ConsoleUtil.getColoredString(ConsoleUtil.color.YELLOW, true, ConsoleUtil.font.BOLD, "W"),
                    content
            );
        if (LogPath != null) {
//            FileOutput("-"+ConsoleUtil.getTime()+" "+name+" [W] "+content);
        }
    }

    public void error(String content) throws IOException {// -[YYYY/MM/dd HH/MM/ss] I xxx
        if (level <= 4)
            System.out.printf(" -%s %s %s %s\n",
                    ConsoleUtil.getColoredString(ConsoleUtil.color.GREEN, false, ConsoleUtil.font.LESS, ConsoleUtil.getTime()),
                    ConsoleUtil.getColoredString(ConsoleUtil.color.CYAN, false, ConsoleUtil.font.LESS, name),
                    ConsoleUtil.getColoredString(ConsoleUtil.color.RED, true, ConsoleUtil.font.BOLD, "E"),
                    content
            );
        if (LogPath != null) {
//            FileOutput("-"+ConsoleUtil.getTime()+" "+name+" [E] "+content.);
        }
    }

    public void fail(String content) throws IOException {// -[YYYY/MM/dd HH/MM/ss] I xxx
        System.out.printf(" -%s %s %s %s\n",
                ConsoleUtil.getColoredString(ConsoleUtil.color.GREEN, false, ConsoleUtil.font.LESS, ConsoleUtil.getTime()),
                ConsoleUtil.getColoredString(ConsoleUtil.color.CYAN, false, ConsoleUtil.font.LESS, name),
                ConsoleUtil.getColoredString(ConsoleUtil.color.PURPLE, true, ConsoleUtil.font.BOLD, "F"),
                ConsoleUtil.getColoredString(ConsoleUtil.color.RED, false, ConsoleUtil.font.BOLD, content)
        );
        if (LogPath != null) {
//            FileOutput("-"+ConsoleUtil.getTime()+" "+name+" [F] "+content);
        }
        throw new RuntimeException();
    }

    public String getName() {
        return name;
    }

    // test
//    public static void main(String[] args) throws Exception {
//        Logger l = new Logger("test");
//        l.level=1;
//        l.info("test");
//        l.warn("test");
//        l.error("test");
//        l.fail("test");
//    }

    public void setName(String name) {
        this.name = name;
    }
}
